class TrafficViolationsController < ApplicationController
  layout 'manage'
  before_action :set_traffic_violation, only: [:show, :edit, :update, :destroy]

  def search
    @traffic_violations = TrafficViolation.search(params[:car], current_user)
    respond_to do |format|
      format.js { render 'js_violation_search', :layout => false }
    end
  end


  # GET /traffic_violations
  # GET /traffic_violations.json
  def index
    @traffic_violations = current_user.traffic_violations.order(created_at: :desc).page params[:page] #TrafficViolation.all
  end

  # GET /traffic_violations/1
  # GET /traffic_violations/1.json
  def show
  end

  # GET /traffic_violations/new
  def new
    @traffic_violation = TrafficViolation.new
  end

  # GET /traffic_violations/1/edit
  def edit
  end

  # POST /traffic_violations
  # POST /traffic_violations.json
  def create
    @traffic_violation = TrafficViolation.new(traffic_violation_params)

    respond_to do |format|
      if @traffic_violation.save
        format.html { redirect_to @traffic_violation, notice: 'Traffic violation was successfully created.' }
        format.json { render :show, status: :created, location: @traffic_violation }
      else
        format.html { render :new }
        format.json { render json: @traffic_violation.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /traffic_violations/1
  # PATCH/PUT /traffic_violations/1.json
  def update
    respond_to do |format|
      if @traffic_violation.update(traffic_violation_params)
        format.html { redirect_to @traffic_violation, notice: 'Traffic violation was successfully updated.' }
        format.json { render :show, status: :ok, location: @traffic_violation }
      else
        format.html { render :edit }
        format.json { render json: @traffic_violation.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /traffic_violations/1
  # DELETE /traffic_violations/1.json
  def destroy
    @traffic_violation.destroy
    respond_to do |format|
      format.html { redirect_to traffic_violations_url, notice: 'Traffic violation was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_traffic_violation
      @traffic_violation = TrafficViolation.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def traffic_violation_params
      params.require(:traffic_violation).permit(:voilated_at, :location, :action, :code, :deduction, :fine_amount, :handled)
    end
end
